package co.y.t.hyfPay;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpClient {
	
	public static String post(String content,String url){
		BufferedReader reader =null;
		BufferedWriter writer =null;
		StringBuffer data = new StringBuffer();
		try {
			URL connect = new URL(url);
			HttpURLConnection connection = (HttpURLConnection)connect.openConnection(); 
 			connection.setRequestMethod("POST");  
				//设置是否向httpUrlConnection输出，因为这个是post请求，参数要放在 
 			//http正文内，因此需要设为true, 默认情况下是false;   
 			connection.setDoOutput(true);
 			//设置是否从httpUrlConnection读入，默认情况下是true; 
 			connection.setDoInput(true);
 			//Post 请求不能使用缓存    
 			connection.setUseCaches(false); 
 	         
 			writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(),"UTF-8"));  
 			writer.write(content);  
 			writer.flush();  
 	        //<===注意，实际发送请求的代码段就在这里
 	        reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
 	        String line;              
 	        while ((line = reader.readLine()) != null) {          
 	            data.append(line);            
 	        }
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally{
			try {
				if(writer!=null){
					writer.close();
				}
				if(reader!=null){
					reader.close(); 
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}  
		}
		return data.toString();
	}
	
}
